/****************************************************************************************
 Copyright © 2003-2012 ZTEsoft Corporation. All rights reserved. Reproduction or       <br>
 transmission in whole or in part, in any form or by any means, electronic, mechanical <br>
 or otherwise, is prohibited without the prior written consent of the copyright owner. <br>
 ****************************************************************************************/
package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.common.util.StrTools;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Objects;

/**
 * <Description> <br>
 *  折扣
 * @author chen.cheng110<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2021年4月6日 <br>
 * @since R9.0<br>
 * @see com.iwhalecloud.bss.kite.cucc.service.attr.handler <br>
 */

public class FeeAgreementMonthlyRentAttrHandler extends AbstractAttrHandler<KiteInst, AttrData> {

    @Autowired
    OperateInstAttrService operateInstAttrService;

    @Override
    public void set(KiteAttr attr, KiteInst prodInst, String value, AttrData data) {
        if (KiteStringUtils.isEmpty(value)) {
            return;
        }
        // 标准月租
        KiteAttr standardMonthlyRentAttr = prodInst.getAttr(LocalKeyConsts.STANDARD_MONTHLY_RENT);
        // 协议月租
        KiteAttr agreementMonthlyRentAttr = prodInst.getAttr(LocalKeyConsts.AGREEMENT_MONTHLY_RENT);
        // 协议月租有值时以协议月租为准，不再根据折扣重新计算
        if (Objects.nonNull(standardMonthlyRentAttr)&&Objects.nonNull(agreementMonthlyRentAttr)&&KiteStringUtils.isNotEmpty(agreementMonthlyRentAttr.getValue())) {
            attr.setValue(value);
            attr.setValueDesc(value);
            return;
        }else if("200001352".equals(attr.getFieldName())){
            attr.setValue(value);
            attr.setValueDesc(value);
        }
        if (Objects.nonNull(standardMonthlyRentAttr)&&Objects.nonNull(agreementMonthlyRentAttr)) {
            if (KiteStringUtils.isEmpty(standardMonthlyRentAttr.getValue()) || KiteStringUtils.equals("无", standardMonthlyRentAttr.getValue())) {
                standardMonthlyRentAttr.setValue("0.00");
            }
            String persentDiscount = StrTools.numDivide(value, "100");
            String agreementPrice = StrTools.numMultiply(persentDiscount, standardMonthlyRentAttr.getValue());
            agreementMonthlyRentAttr.setValue(agreementPrice);
            agreementMonthlyRentAttr.setValueDesc(agreementPrice);
            attr.setValue(value);
            attr.setValueDesc(value);
        }
    }
}
